package org.ccay.core.encrypt.impl;

import java.io.UnsupportedEncodingException;

import org.apache.commons.codec.binary.Base64;
import org.ccay.core.log.CcayLoggerFactory;
import org.ccay.core.log.ILogger;
import org.ccay.core.util.AssertUtil;
import org.ccay.core.util.EncodingUtil;
import org.ccay.core.util.StringUtil;

/**
 * 加密解密辅助工具	
 * @author chaowangbang
 * @since 2012-3-7
 */
public final class EncryptUtil {
	private static ILogger logger = CcayLoggerFactory.getLogger(EncryptUtil.class);
	
	/**
	 * Avoid Instances
	 */
	private EncryptUtil(){
	}
	
	/**
	 * 默认KEY大小
	 */
	private static final int KEY_SIZE = 16;

	/** 
	 * 生成KEY
	 * @param original 原始字符串
	 * @return
	 */
	public static String toKey(String original){
		AssertUtil.assertStringNotEmpty(original, "key");
		
		if(original.length() > KEY_SIZE){
			original = original.substring(0,KEY_SIZE);
		}else{
			original = StringUtil.padLeft(original, KEY_SIZE, '0');
		}
		
		try {
			return Base64.encodeBase64String(original.getBytes(EncodingUtil.UTF_8));
		} catch (UnsupportedEncodingException e) {
			logger.error(e);
			return null;
		}
	}
}
